* Do file to organize RTA data
* written in Stata version 10.1
* First version of code Oct 10, 2008
* JBS October 15, 2008 
* Revised Jan 15 2009-jg
* additional revision march 9 11- jg

clear 

* Load Data:
* "surveydata_nobias_11oct11.csv is the original data with responses removed where the respondent answered with regard to his/her own RTA. These are the 
* data used in the Bayesian factor analysis.  

   
insheet using surveydata_nobias_11oct11.csv


* The following code makes string variables into numeric variables with labels
encode region, gen(region2)
drop region
rename region2 region
* Drop Bilateral Agreements (Some bilaterals were scored, but we do not use them in our analysis)
drop if region==3

encode rta, gen(rta2)
drop rta
rename rta2 rta
  
encode dimension, gen(dimension2)
drop dimension
rename dimension2 dimension

*We drop the PTA Strong-Weak dimension. This is necessary to reshape the data because "scale" is missing. No distinction made between position and importance.
drop if dimension == 16

* create id variable -- necessary for reshaping
egen id = group(respondent rtaname dimension)

* We reshape data to make the unit of analysis RTA/Dimension. This allows us to calculate summary statistics for each RTA on each dimension
reshape wide score respondent, i(id) j(scale) string

* create a variable to count the number of respondents for each rta/dimension
bys rta dimension: gen numrespondents = _N
bys rta dimension: egen meanscore = mean(scoreDegree)
bys rta dimension: egen sdscore = sd(scoreDegree) 

bys rta dimension: egen meansal = mean(scoreImportance) 
bys rta dimension: egen sdsal = sd(scoreImportance)

collapse meanscore sdscore meansal sdsal numres, by (rta dimension)


***********************************************************************
* The code below reshapes the (collapsed) data wide to make RTA the unit of analysis.  
***********************************************************************

*Codes for row variable names (dimensions)
* 1 Ability to meet own goals
* 2 Ambition of goals
* 3 Delegation
* 4 Dispute settlement
* 5 Enforcement
* 6 Escape clauses
* 7 Internal tariff reduction
* 8 International influence
* 9 Intra-PTA trade
* 10 Legalization
* 11 Market access in goods
* 12 Market access in services
* 13 Monitoring 
* 14 Non-tariff barriers
* 15 Obligation
* 17 Political influence
* 18 Precision
* 19 Quality of infrastructure/secretariat 
* 20 Rules of Origin 
* 21 Scope of goals
* 22 Tariff reduction
* 23 Trade diversion
* 24 Trade negotiator capacity
* 25 WTO Compliance

drop numres

reshape wide meanscore sdscore meansal sdsal, i(rta) j(dimension) 


label var  meanscore1 "	Ability to meet own goals(Mean Score)"
label var    sdscore1 "	Ability to meet own goals(SD Score)"
label var    meansal1 "	Ability to meet own goals(Mean Importance)"
label var      sdsal1 "	Ability to meet own goals(SD Importance)"
label var  meanscore2 "	Ambition of goals(Mean Score)"
label var    sdscore2 "	Ambition of goals(SD Score)"
label var    meansal2 "	Ambition of goals(Mean Importance)"
label var      sdsal2 "	Ambition of goals(SD Importance)"
label var  meanscore3 "	Delegation(Mean Score)"
label var    sdscore3 "	Delegation(SD Score)"
label var    meansal3 "	Delegation(Mean Importance)"
label var      sdsal3 "	Delegation(SD Importance)"
label var  meanscore4 "	Dispute settlement(Mean Score)"
label var    sdscore4 "	Dispute settlement(SD Score)"
label var    meansal4 "	Dispute settlement(Mean Importance)"
label var      sdsal4 "	Dispute settlement(SD Importance)"
label var  meanscore5 "	Enforcement(Mean Score)"
label var    sdscore5 "	Enforcement(SD Score)"
label var    meansal5 "	Enforcement(Mean Importance)"
label var      sdsal5 "	Enforcement(SD Importance)"
label var  meanscore6 "	Escape clauses(Mean Score)"
label var    sdscore6 "	Escape clauses(SD Score)"
label var    meansal6 "	Escape clauses(Mean Importance)"
label var      sdsal6 "	Escape clauses(SD Importance)"
label var  meanscore7 "	Internal tariff reduction(Mean Score)"
label var    sdscore7 "	Internal tariff reduction(SD Score)"
label var    meansal7 "	Internal tariff reduction(Mean Importance)"
label var      sdsal7 "	Internal tariff reduction(SD Importance)"
label var  meanscore8 "	International influence(Mean Score)"
label var    sdscore8 "	International influence(SD Score)"
label var    meansal8 "	International influence(Mean Importance)"
label var      sdsal8 "	International influence(SD Importance)"
label var  meanscore9 "	Intra-PTA trade(Mean Score)"
label var    sdscore9 "	Intra-PTA trade(SD Score)"
label var    meansal9 "	Intra-PTA trade(Mean Importance)"
label var      sdsal9 "	Intra-PTA trade(SD Importance)"
label var meanscore10 "	Legalization(Mean Score)"
label var   sdscore10 "	Legalization(SD Score)"
label var   meansal10 "	Legalization(Mean Importance)"
label var     sdsal10 "	Legalization(SD Importance)"
label var meanscore11 "	Market access in goods(Mean Score)"
label var   sdscore11 "	Market access in goods(SD Score)"
label var   meansal11 "	Market access in goods(Mean Importance)"
label var     sdsal11 "	Market access in goods(SD Importance)"
label var meanscore12 "	Market access in services(Mean Score)"
label var   sdscore12 "	Market access in services(SD Score)"
label var   meansal12 "	Market access in services(Mean Importance)"
label var     sdsal12 "	Market access in services(SD Importance)"
label var meanscore13 "	Monitoring(Mean Score)"
label var   sdscore13 "	Monitoring(SD Score)"
label var   meansal13 "	Monitoring(Mean Importance)"
label var     sdsal13 "	Monitoring(SD Importance)"
label var meanscore14 "	Non-tariff barriers(Mean Score)"
label var   sdscore14 "	Non-tariff barriers(SD Score)"
label var   meansal14 "	Non-tariff barriers(Mean Importance)"
label var     sdsal14 "	Non-tariff barriers(SD Importance)"
label var meanscore15 "	Obligation(Mean Score)"
label var   sdscore15 "	Obligation(SD Score)"
label var   meansal15 "	Obligation(Mean Importance)"
label var     sdsal15 "	Obligation(SD Importance)"
label var meanscore17 "	Political influence(Mean Score)"
label var   sdscore17 "	Political influence(SD Score)"
label var   meansal17 "	Political influence(Mean Importance)"
label var     sdsal17 "	Political influence(SD Importance)"
label var meanscore18 "	Precision(Mean Score)"
label var   sdscore18 "	Precision(SD Score)"
label var   meansal18 "	Precision(Mean Importance)"
label var     sdsal18 "	Precision(SD Importance)"
label var meanscore19 "	Quality of infrastructure/secretariat(Mean Score)"
label var   sdscore19 "	Quality of infrastructure/secretariat(SD Score)"
label var   meansal19 "	Quality of infrastructure/secretariat(Mean Importance)"
label var     sdsal19 "	Quality of infrastructure/secretariat(SD Importance)"
label var meanscore20 "	Rules of Origin(Mean Score)"
label var   sdscore20 "	Rules of Origin(SD Score)"
label var   meansal20 "	Rules of Origin(Mean Importance)"
label var     sdsal20 "	Rules of Origin(SD Importance)"
label var meanscore21 "	Scope of goals(Mean Score)"
label var   sdscore21 "	Scope of goals(SD Score)"
label var   meansal21 "	Scope of goals(Mean Importance)"
label var     sdsal21 "	Scope of goals(SD Importance)"
label var meanscore22 "	Tariff reduction(Mean Score)"
label var   sdscore22 "	Tariff reduction(SD Score)"
label var   meansal22 "	Tariff reduction(Mean Importance)"
label var     sdsal22 "	Tariff reduction(SD Importance)"
label var meanscore23 "	Trade diversion(Mean Score)"
label var   sdscore23 "	Trade diversion(SD Score)"
label var   meansal23 "	Trade diversion(Mean Importance)"
label var     sdsal23 "	Trade diversion(SD Importance)"
label var meanscore24 "	Trade negotiator capacity(Mean Score)"
label var   sdscore24 "	Trade negotiator capacity(SD Score)"
label var   meansal24 "	Trade negotiator capacity(Mean Importance)"
label var     sdsal24 "	Trade negotiator capacity(SD Importance)"
label var meanscore25 "	WTO compliance(Mean Score)"
label var   sdscore25 "	WTO compliance(SD Score)"
label var   meansal25 "	WTO compliance(Mean Importance)"
label var     sdsal25 "	WTO compliance(SD Importance)"

* Now we have created the full, reshaped, dataset. We save it here.
save "rta_data_full.dta",replace

* Now create file for reading into R containing only variables we need
keep rta meanscore* meansal*

* Here we rescale the means and multiply by salience as described in the paper.
gen Ability = (meanscore1-5.5)*meansal1
gen Ambition = (meanscore2-5.5)*meansal2
gen Delegation = (meanscore3-5.5)*meansal3
gen DSM = (meanscore4-5.5)*meansal4
gen Enforcement = (meanscore5-5.5)*meansal5
gen Escape = (meanscore6-5.5)*meansal6
gen Tariffinternal = (meanscore7-5.5)*meansal7
gen International = (meanscore8-5.5)*meansal8
gen IntraPTA = (meanscore9-5.5)*meansal9
gen Legalization = (meanscore10-5.5)*meansal10
gen Goods = (meanscore11-5.5)*meansal11
gen Services = (meanscore12-5.5)*meansal12
gen Monitoring = (meanscore13-5.5)*meansal13
gen NTBs = (meanscore14-5.5)*meansal14
gen Obligation = (meanscore15-5.5)*meansal15
gen Political = (meanscore17-5.5)*meansal17
gen Precision = (meanscore18-5.5)*meansal18
gen Secretariat = (meanscore19-5.5)*meansal19
gen ROO =(meanscore20-5.5)*meansal20
gen Scope = (meanscore21-5.5)*meansal21
gen Tariffexternal=(meanscore22-5.5)*meansal22
gen Tradediversion = (meanscore23-5.5)*meansal23
gen Negotiator = (meanscore24-5.5)*meansal24
gen WTO = (meanscore25-5.5)*meansal25

* Drop variables we don't need
drop meanscore* meansal*

* Save data for use in R. We must use saveold command as R can only read in Stata 10 files 
saveold "rta_meanXsal.dta",replace



